package com.wjk.kylin.modules.system.config.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.config.service.IConfigService;
import com.wjk.kylin.modules.system.config.entity.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

/**
 * <p>
 *  参数配置表 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-07
 */
@Api("参数配置")
@RestController
@RequestMapping("/sys/config")
public class ConfigController extends BaseController {

    @Autowired
    private IConfigService configService;

    @PreAuthorize("hasAuthority('system:config:list')")
    @ApiOperation("列表")
    @Log(title = "参数配置-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(Config config) {
        IPage<Config> page = configService.findPage(new RequestData<Config>(), config);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:config:query')")
    @ApiOperation("详情")
    @Log(title = "参数配置-详情",businessType = GET)
    @GetMapping("/{configId}")
    public ResponseData get(@PathVariable String configId) {
        Config config = configService.getById(configId);
        return ResponseData.success().data("config", config);
    }

    @PreAuthorize("hasAuthority('system:config:add')")
    @ApiOperation("新增")
    @Log(title = "参数配置-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody Config config) {
        boolean flag = configService.save(config);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:config:edit')")
    @ApiOperation("编辑")
    @Log(title = "参数配置-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody Config config) {
        boolean flag = configService.update(config);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:config:delete')")
    @ApiOperation("删除")
    @Log(title = "参数配置-删除",businessType = DELETE)
    @DeleteMapping("/{configIds}")
    public ResponseData delete(@PathVariable String[] configIds) {

        boolean flag = configService.deleteByIds(configIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }

    @PreAuthorize("hasAuthority('system:config:delete')")
    @ApiOperation("清空缓存")
    @Log(title = "参数配置-清空缓存", businessType = CLEAN)
    @DeleteMapping("/clearCache")
    public ResponseData clearCache() {
        configService.clearCache();
        return ResponseData.success();
    }

    @ApiOperation("根据参数键名查询参数值")
    @GetMapping(value = "/configKey/{configKey}")
    public ResponseData getConfigKey(@PathVariable String configKey) {
        return ResponseData.success().data("value",configService.getConfigKey(configKey));
    }

}
